package com.cictec.middleware.tde.rm.protocol.rm.decode;

import java.nio.ByteBuffer;

import org.apache.mina.core.session.IoSession;

import com.cictec.middleware.commons.utils.BinaryUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.tde.exception.MessageDecoderException;
import com.cictec.middleware.tde.rm.protocol.AbstractMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage.Header;
import com.cictec.middleware.tde.rm.protocol.annotion.TerminalMessageDecoder;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x02TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.utils.RmBinaryUtils;

/**
 * 0x02 上传 GPS 定位数据
 * 
 * @author Administrator
 *
 */
@TerminalMessageDecoder(0x02)
public class Rm0x02Decoder extends AbstractMessageDecoder {

	@Override
	public TerminalMessage decode(Header header, ByteBuffer in, IoSession session) throws MessageDecoderException {

		String decoderFlag = PropertiesUtils.getString("terimial.decoder.positionDecoder", "default");

		// 呼市位置信息解析
		if (decoderFlag.equals("hs")) {
			return hushi(header, in);
		}

		return defaultHandler(header, in);
	}

	/**
	 * 通用处理
	 * 
	 * @param header
	 * @param in
	 * @return
	 */
	private Rm0x02TerminalMessage defaultHandler(Header header, ByteBuffer in) {

		Rm0x02TerminalMessage tp = new Rm0x02TerminalMessage(header);
		tp.setLat(RmBinaryUtils.dwordToLatLngRM(in));
		tp.setLng(RmBinaryUtils.dwordToLatLngRM(in));
		tp.setSpeed(BinaryUtils.unsignedByteToInt(in));
		tp.setAngle(BinaryUtils.unsignedByteToInt(in));
		tp.setAltitude(BinaryUtils.wordToInt(in));
		tp.setBusTemperature(BinaryUtils.unsignedByteToInt(in));
		tp.setEngineTemperature(BinaryUtils.unsignedByteToInt(in));
		tp.setEngineTemperature(BinaryUtils.unsignedByteToInt(in));
		tp.setGpsOdometer(BinaryUtils.dwordToLong(in));
		tp.setCanBusProtocol(BinaryUtils.unsignedByteToInt(in));
		tp.setCanBusDataLongth(BinaryUtils.unsignedByteToInt(in));
		if (tp.getCanBusDataLongth() != 0) {
			tp.setCanBusData(RmBinaryUtils.dataToBusDataRM(in, tp.getCanBusDataLongth()));
		}
		tp.setVoltage1(BinaryUtils.wordToInt(in));
		tp.setCurrent1(BinaryUtils.wordToInt(in));
		tp.setPower1(BinaryUtils.wordToInt(in));
		tp.setEnergy1(BinaryUtils.dwordToLong(in));
		tp.setVoltage2(BinaryUtils.wordToInt(in));
		tp.setCurrent2(BinaryUtils.wordToInt(in));
		tp.setPower2(BinaryUtils.wordToInt(in));
		tp.setEnergy2(BinaryUtils.dwordToLong(in));
		tp.setTemperature(BinaryUtils.dwordToLong(in));
		tp.setHumidity(BinaryUtils.dwordToLong(in));
		tp.setReserved1(BinaryUtils.dwordToLong(in));
		tp.setPositionModel(BinaryUtils.unsignedByteToInt(in));
		tp.setReserved2(BinaryUtils.wordToInt(in));
		return tp;
	}

	/**
	 * 呼市协议处理
	 * 
	 * @param header
	 * @param in
	 * @return
	 */
	private Rm0x02TerminalMessage hushi(Header header, ByteBuffer in) {
		Rm0x02TerminalMessage tp = new Rm0x02TerminalMessage(header);

		tp.setLat(RmBinaryUtils.dwordToLatLngRM(in));
		tp.setLng(RmBinaryUtils.dwordToLatLngRM(in));
		tp.setSpeed(BinaryUtils.unsignedByteToInt(in));
		tp.setAngle(BinaryUtils.unsignedByteToInt(in));
		tp.setAltitude(BinaryUtils.wordToInt(in));
		tp.setBusTemperature(BinaryUtils.unsignedByteToInt(in));
		tp.setEngineTemperature(BinaryUtils.unsignedByteToInt(in));
		tp.setEngineTemperature(BinaryUtils.unsignedByteToInt(in));
		tp.setGpsOdometer(BinaryUtils.dwordToLong(in));

		// tp.setCanBusProtocol(BinaryUtils.unsignedByteToInt(in));
		// tp.setCanBusDataLongth(BinaryUtils.unsignedByteToInt(in));

		if (header.getBodyLength() >= 83) {
			tp.setCanBusProtocol(BinaryUtils.unsignedByteToInt(in));
			tp.setCanBusDataLongth(BinaryUtils.unsignedByteToInt(in));
			if (tp.getCanBusDataLongth() != 0) {
				tp.setCanBusData(RmBinaryUtils.dataToBusDataRM(in, tp.getCanBusDataLongth()));
			}

		} else {// 呼市的老设备 ,版本不一致，字节长度不一样
			in.get(new byte[4]);
		}

		/*
		 * tp.setVoltage1(BinaryUtils.wordToInt(in));
		 * tp.setCurrent1(BinaryUtils.wordToInt(in));
		 * tp.setPower1(BinaryUtils.wordToInt(in));
		 * tp.setEnergy1(BinaryUtils.dwordToLong(in));
		 * tp.setVoltage2(BinaryUtils.wordToInt(in));
		 * tp.setCurrent2(BinaryUtils.wordToInt(in));
		 * tp.setPower2(BinaryUtils.wordToInt(in));
		 * tp.setEnergy2(BinaryUtils.dwordToLong(in));
		 * tp.setTemperature(BinaryUtils.dwordToLong(in));
		 * tp.setHumidity(BinaryUtils.dwordToLong(in));
		 * tp.setReserved1(BinaryUtils.dwordToLong(in));
		 */
		if (header.getBodyLength() > 83) {// 区分老版本 呼市的版本没有北斗卫星设置，少一个字节
			tp.setPositionModel(BinaryUtils.unsignedByteToInt(in));
		}
		tp.setReserved2(BinaryUtils.wordToInt(in));

		return tp;
	}
}
